package com.zhsm.fuxi.mvp.presenter

import com.hankkin.library.mvp.presenter.RxLifePresenter
import com.hankkin.reading.control.UserControl
import com.hankkin.reading.http.HttpClientUtils
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import com.zhsm.fuxi.app.Constant
import com.zhsm.fuxi.utils.QiniuUpLoadHelper
import com.zhsm.fuxi.mvp.contract.MyCenterContract
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * Created by GuZhC on 2018/10/9.
 * describe:
 */
class MyCenterPresenter : RxLifePresenter<MyCenterContract.IView>(), MyCenterContract.IPresenter {

    val mQiniuUpLoadHelper: QiniuUpLoadHelper by lazy {
        QiniuUpLoadHelper(Constant.QiNiu.SYSTEM)
    }

    override fun loginOut() {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().loginOut(UserControl.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().loginOutSucess()
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun updaateUserIn() {
        HttpClientUtils.Builder.getPaipiaHttp().updateUserInfo(UserControl.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().updaateUserInfoSuccess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun changeNick(nick: String) {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().changeNick(UserControl.getId(),nick)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().saveNickSucces(nick)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }


    override fun changeEmail(email : String) {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().changeEmail(UserControl.getId(),email)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().saveEmailSucces(email)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun changePhone(phone: String) {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().changePhone(UserControl.getId(),phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().savePhoneSucces(phone)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun changeHeadPic(picPath : String) {
        getMvpView().showLoding()
        mQiniuUpLoadHelper.filePath = picPath
        mQiniuUpLoadHelper!!.setUpLoadPicListener(object : QiniuUpLoadHelper.UpLoadPicListener{
            override fun onSuccess(key: String) {
                HttpClientUtils.Builder.getPaipiaHttp().changeHeadPhoto(UserControl.getId(),key)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeEx({
                            getMvpView().changeHeadPicSucces(key)
                        }, {
                            getMvpView().hideLoding()
                        }).bindRxLifeEx(RxLife.ON_DESTROY)
            }
            override fun onError(msg: String) {
                ToastUtils.makeText(msg)
                getMvpView().hideLoding()
            }
        })
        mQiniuUpLoadHelper!!.uploadPic()
    }

    override fun onDestroy() {
        super.onDestroy()
        mQiniuUpLoadHelper?.let {
            it.setCancle(true)
            it.setUpLoadPicListener(null)
        }

    }
}